<?php
declare(strict_types=1);

namespace app\common\business;

use app\common\model\mysql\OrderGoods as OrderGoodsModel;
use think\Exception;

class OrderGoods extends BusiBase
{
    public function __construct()
    {
        $this->model = new OrderGoodsModel();
    }

    /**
     * 多条数据 添加
     *
     * @param array $data
     * @return array
     */
    public function addAll(array $data)
    {
        if (!$data) return [];
        try {
            $result = $this->model->saveAll($data);
            return $result->toArray();
        } catch (\Exception $e) {
            $this->errorLog('saveall error: '.$e->getMessage());
            throw new Exception('服务器内部错误');
        }
    }

    /**
     * 根据订单号获取商品信息
     *
     * @param int $orderId
     * @return array
     */
    public function getDataByCondition(int $orderId)
    {
        if (!$orderId) return [];
        $where = ['order_id' => $orderId];
        try {
            $res = $this->model->getDataByCondition(['order_id' => $orderId]);
        } catch (\Exception $e) {
            $this->errorLog('get data by order_id='.$orderId." error: ".$e->getMessage());
            return [];
        }
        if (!$res) return [];
        return $res->toArray();
    }
}